package com.designCreator.model
{
	import com.designCreator.model.business.DesignCreatorDelegate;
	import com.designCreator.model.vo.DesignCreatorRequest;
	import com.designCreator.notifications.NotificationNames;
	import com.designCreator.utils.Constants;
	
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import mx.collections.XMLListCollection;
	import mx.controls.Alert;
	import mx.rpc.AsyncToken;
	import mx.rpc.IResponder;
	import mx.rpc.events.ResultEvent;
	import mx.utils.ObjectUtil;
	
	import org.puremvc.as3.interfaces.IProxy;
	import org.puremvc.as3.patterns.proxy.Proxy;
	
	public class DesignCreatorProxy extends Proxy implements IProxy, 
		IResponder
	{
		public static const NAME : String = "DesignCreatorProxy";
		
		public var solidFabricDesignData:XMLListCollection ;
		public var whiteFabricDesignData:XMLListCollection ;
		public var patternDesignData:XMLListCollection ;
		
		public var fabricSubDesgnData:XMLListCollection ;
		public var styleDesignData:XMLListCollection ;
		
		public function DesignCreatorProxy( data : Object = null )
		{
			super( NAME, data );
		}
		
		public function result( event : Object ) : void
		{
			//feed.assign( ResultEvent( event ).result as Feed );
			//sendNotification( NotificationNames.LOAD_FEED_SUCCESS );
		}
		
		public function fault( event : Object ) : void
		{
			//sendNotification( NotificationNames.LOAD_FEED_FAILURE );
		}
		
		/*public function loadWhiteFabricsXML(path:String)
		public function loadSolidFabricsXML(path:String)
		public function loadPatternFabricsXML(path:String)
		public function loadAllFabricsXML(path:String)*/
		
		
		public function loadDummySolidFabricsData(requestVO:DesignCreatorRequest):void
		{
			var L1Path:String = requestVO.solidFabricDesignsXMLPath ;
			
			if(L1Path == null || L1Path.length==0)
				return ;
			
			var urlLoader:URLLoader = new URLLoader()
			var urlReq:URLRequest = new URLRequest(L1Path);
			urlLoader.addEventListener(Event.COMPLETE,onloadDummySolidFabricsData);
			urlLoader.addEventListener(ErrorEvent.ERROR,onErrorLoadDummySolidFabricsData);
			urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,onConfigFileLoadSecurityError);
			urlLoader.load(urlReq); 
		}
		
		private function onloadDummySolidFabricsData(event:Event):void
		{
			var xml:XML = XML(URLLoader(event.currentTarget).data);
			
			if(xml!=null)
			{
				solidFabricDesignData = new XMLListCollection(xml.Solid_Fabrics);
				setData(solidFabricDesignData);
				sendNotification(NotificationNames.LOAD_SOLID_FABRIC_DATA_SUCCESS,solidFabricDesignData);
				//loadL2Configuration(L2DesignXmlPath);
			}
			else
			{
				solidFabricDesignData = new XMLListCollection();
				setData(solidFabricDesignData);
				sendNotification(NotificationNames.LOAD_SOLID_FABRIC_DATA_ERROR,solidFabricDesignData);
			}
		} 
		
		private function onErrorLoadDummySolidFabricsData(event:ErrorEvent):void
		{
			sendNotification(NotificationNames.LOAD_SOLID_FABRIC_DATA_ERROR);
		}
		
		/**White fabric designs */
		public function loadDummyWhiteFabricsData(requestVO:DesignCreatorRequest):void
		{
			var L1Path:String = requestVO.whiteFabricDesignsXMLPath ;
			
			if(L1Path == null || L1Path.length==0)
				return ;
			
			var urlLoader:URLLoader = new URLLoader()
			var urlReq:URLRequest = new URLRequest(L1Path);
			urlLoader.addEventListener(Event.COMPLETE,onloadDummyWhiteFabricsData);
			urlLoader.addEventListener(ErrorEvent.ERROR,onErrorLoadDummyWhiteFabricsData);
			urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,onConfigFileLoadSecurityError);
			urlLoader.load(urlReq); 
		}
		
		private function onloadDummyWhiteFabricsData(event:Event):void
		{
			var xml:XML = XML(URLLoader(event.currentTarget).data);
			
			if(xml!=null)
			{
				whiteFabricDesignData = new XMLListCollection(xml.White_Fabrics);
				setData(whiteFabricDesignData);
				sendNotification(NotificationNames.LOAD_WHITE_FABRIC_DATA_SUCCESS,whiteFabricDesignData);
				//loadL2Configuration(L2DesignXmlPath);
			}
			else
			{
				whiteFabricDesignData = new XMLListCollection();
				setData(whiteFabricDesignData);
				sendNotification(NotificationNames.LOAD_WHITE_FABRIC_DATA_ERROR,whiteFabricDesignData);
			}
		} 
		
		private function onErrorLoadDummyWhiteFabricsData(event:ErrorEvent):void
		{
			sendNotification(NotificationNames.LOAD_WHITE_FABRIC_DATA_ERROR);
		}
		
		
		private function onConfigFileLoadSecurityError(event:SecurityErrorEvent):void
		{
			/*Alert.show(
				resourceManager.getString(Constants.MSG_RESOURCE,'CONFIG_FILE_LOAD_SECURITY_ERROR'),
				resourceManager.getString(Constants.MSG_RESOURCE,'ERROT_TITLE'));*/
		}
		
		/*public function set feed( value : Feed ) : void
		{
			data = value;
			if ( !value.loaded )
			{
				var delegate : LoadFeedDelegate = new LoadFeedDelegate();
				var token : AsyncToken = delegate.send( value.url );
				token.addResponder( this );
			}
		}*/
		
		//--------------
		//load solid fabric, result and fault handler
		//------------
		public function loadSolidFabricsDesign(requestVO:DesignCreatorRequest):void
		{
			//uncomment to call service from php and comment dummy value to fetch
			/*var delegate : DesignCreatorDelegate = new DesignCreatorDelegate();
			var token : AsyncToken = delegate.loadSolidFabricsDesign(requestVO);
			token.addResponder( this );*/
			loadDummySolidFabricsData(requestVO);
		}	
	
		//--------------
		//load white fabric, result and fault handler
		//------------
		public function loadWhiteFabricsDesign(requestVO:DesignCreatorRequest):void
		{
			//uncomment to call service from php and comment dummy value to fetch
			/*var delegate : DesignCreatorDelegate = new DesignCreatorDelegate();
			var token : AsyncToken = delegate.loadWhiteFabricsDesign(requestVO);
			token.addResponder( this );*/
			loadDummyWhiteFabricsData(requestVO);
		}	
	}
}